﻿'use strict';
app.controller('ProfileController', function ($rootScope, $scope, $state, $localStorage, $http, $modal, commonFactory) {
    if ($localStorage.user == null) {
        $state.go('login');
    }
    
    $rootScope.user = {},
    GetUser($localStorage.user.UserId);
    function GetUser(UserId) {
        commonFactory.getUserById(UserId).then(function (response) {
            $rootScope.user = response.data;
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
    }
})
app.controller('EditProfileController', function ($rootScope, $scope, $localStorage, $http, $modal, commonFactory, profileFactory) {
    $scope.processing = false;    
    $scope.bank = {},
    $scope.BankList = [],
    $scope.BankName = "",
    BankList(),
    GetBank($localStorage.user.UserId),
    $scope.doEditProfile = function (DataProfile) {
        $scope.processing = true;
        profileFactory.Edit(DataProfile).then(function (response) {            
            $scope.messeger = response.data;
            $scope.processing = false;
            if (response.data.Status) {
                $scope.popup("modal-message modal-success");
            }else{
                $scope.popup("modal-message modal-warning");
            }            
        }, function (error) {
            $scope.status = 'Unable to load country data: ' + error.message;
            $scope.processing = false;
        });
    },
    $scope.title = "Edit Profile",
    $scope.messeger = "",
    $scope.popup = function (windowClass) {
        $modal.open({
            windowClass: windowClass,
            templateUrl: "WarningModal",
            controller: "IModalInstanceCtrl",
            size: "sm",
            resolve: {
                messeger: function () {
                    return $scope.messeger;
                },
                title: function () {
                    return $scope.title;
                }
            }
        })
    }
    function BankList() {
        commonFactory.getBankList().then(function (rsp) {
            if (rsp.data.Status == true) {
                $scope.BankList = rsp.data.Result;
            }
        }, function (error) {
            $scope.status = 'Unable to load Bank data: ' + error.message;
        });
    };
    function GetBank(userId) {
        commonFactory.getBank(userId).then(function (rsp) {
            if (rsp.data.Status == true) {
                $scope.bank.selected = { Name: rsp.data.Result.Name };
            }
        }, function (error) {
            $scope.status = 'Unable to load Bank data: ' + error.message;
        });
    };
})
app.controller('ChangepasswordController', function ($rootScope, $scope, $state, $localStorage, $http, $modal, profileFactory) {
    $scope.processing = false;
    $scope.title = "Change Password",
    $scope.messeger = "",
    $scope.popup = function (windowClass){
        $modal.open({
            windowClass: windowClass,
            templateUrl: "WarningModal",
            controller: "IModalInstanceCtrl",
            size: "sm",
            resolve: {
                messeger: function() {
                    return $scope.messeger;
                },
                title: function() {
                    return $scope.title;
                }
            }
        })
    },
    $scope.data = { UserId: $localStorage.user.UserId, Password: "", Password1: "", Password2: "" },
    $scope.doChangePass = function (data) {
        $scope.messeger="",
        $scope.processing = true;
        changePassword(data);
    }
    function changePassword(data) {
        profileFactory.changePassword(data).then(function (resp) {
            if (resp.data.Status) {
                $scope.messeger = resp.data.Messeger;
            }
            else {
                $scope.messeger = resp.data.Messeger;                
            }            
            $scope.processing = false;
        }, function (error) {
            $scope.messeger = 'Unable to load change data: ' + error.message;
            $scope.processing = false;
        });
        
    }
    
})
app.controller('ChangeSecondPasswordController', function ($rootScope, $scope, $state, $localStorage, $http, $modal, profileFactory) {
    $scope.processing = false;
    $scope.title = "Change Password Level 2",
    $scope.messeger = "",
    $scope.popup = function (windowClass) {
        $modal.open({
            windowClass: windowClass,
            templateUrl: "WarningModal",
            controller: "IModalInstanceCtrl",
            size: "sm",
            resolve: {
                messeger: function () {
                    return $scope.messeger;
                },
                title: function () {
                    return $scope.title;
                }
            }
        })
    },
    $scope.data = { UserId: $localStorage.user.UserId, SecondPassword: "", SecondPassword1: "", SecondPassword2: "" },
    $scope.doChangePass2 = function (data) {
        $scope.messeger = "",
        $scope.processing = true;
        changePassword2(data);
    }
    function changePassword2(data) {
        profileFactory.changePassword2(data).then(function (resp) {
            if (resp.data.Status) {
                $scope.messeger = resp.data.Messeger;
            }
            else {
                $scope.messeger = resp.data.Messeger;
            }
            $scope.processing = false;
        }, function (error) {
            $scope.messeger = 'Unable to load change data: ' + error.message;
            $scope.processing = false;
        });

    }

})
app.controller('PremiumEditController', function ($scope, $http, $rootScope, $modal, $localStorage, $state, $timeout, commonFactory, BitcoinFactory) {
    $scope.processing = false;
    $scope.user = $localStorage.user,
    $scope.payment = {},
    $scope.wallet = {},
    $scope.WalletList = [],
    $scope.Payments = [],
    $scope.selected = [],
    $scope.showeditperfectmoney = false,
    $scope.processing = false,
    $scope.showeditbitcoin = false,
    $scope.title = "Bitcoin Edit",
    $scope.messeger = "",
    $scope.showChange = false,
    $scope.popup = function (windowClass) {
        $modal.open({
            windowClass: windowClass,
            templateUrl: "WarningModal",
            controller: "IModalInstanceCtrl",
            size: "sm",
            resolve: {
                messeger: function () {
                    return $scope.messeger;
                },
                title: function () {
                    return $scope.title;
                }
            }
        })
    },
    $scope.Profile = {
        WalletId: '',
        PaymentId: ''
    },
    $scope.Account = {
        UserId: $scope.user.UserId,
        email: $scope.user.Email,
        Payee_Account: '',
        Payer_Account: '',
        PinCode: '',
        WalletId: '',
    },
    WalletWithBalance($scope.user.UserId);
    function WalletWithBalance(UserId) {
        commonFactory.GetNotPeddingWalletList(UserId).then(function (response) {
            $scope.WalletList = response.data.Result;
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
    };
    $http.post($rootScope.APITransactionUrl + 'GetPaymentMethod').then(function successCallback(resp) {
        $scope.Payments = resp.data.Result;
        $scope.selected1 = { value: $scope.Payments[0] };
    });
    $scope.doGetOTP = function () {
        GetOTP();
    };
    processPremiumEdit();
    function processPremiumEdit() {
        GetAccountBitcoinByUserId($scope.user.UserId);
        $scope.showeditbitcoin = true;
    };
    function GetOTP() {
        $scope.processing = true;
        $http.post($rootScope.APIUrl + "validationAccount", { userId: $scope.user.UserId, WalletId: 1, PaymentId: 2 }).then(function successCallback(resp) {
            if (resp.data.Status) {
                $scope.messeger = resp.data.Messeger;
                $scope.showChange = true;
                $scope.processing = false;
                $scope.popup("modal-message modal-success");
            } else {
                $scope.messeger = resp.data.Messeger,
                $scope.popup("modal-message modal-warning");
            }
        }, function errorCallback(resp) {
            $scope.processing = false;
            $scope.messeger = "Error validation Account",
            $scope.popup("modal-message modal-warning");
        })
    };
    function GetAccountBitcoinByUserId(UserId) {
        $scope.title = "Account BitCoin";
        $scope.processing = false;
        $scope.showeditbitcoin = true;
        BitcoinFactory.GetAccountBitcoinByUserId(UserId).then(function successCallback(resp) {
            if (resp.data.Status) {
                $scope.Account.Payer_Account = resp.data.Result.Payer;
            } else {
                $scope.messeger = resp.data.Messeger;
            }
        }, function errorCallback(resp) {

        })
    }
    //change account bitcoin
    $scope.doChangeAccBitcoin = function (Account) {
        $scope.processing = true;
        $scope.title = "Change Account Bitcoin";
        $http.post($rootScope.APIUrl + 'ChangeAccBitCoin', { data: Account }).then(function successCallback(resp) {
            $scope.processing = false;
            if (resp.data.Status) {
                $scope.messeger = resp.data.Messeger,
                $scope.popup("modal-message modal-successs");
            } else {
                $scope.messeger = resp.data.Messeger,
                $scope.popup("modal-message modal-warning");
            }
        }, function errorCallback(resp) {
            $scope.processing = false;
            $scope.messeger = "Contact the Administrator by supporting",
            $scope.popup("modal-message modal-warning");
        })
    }
})
app.controller('UpgradeController', function ($scope, $http, $rootScope, $localStorage, $modal, commonFactory) {
    $scope.WalletList = [],
    $scope.UserLevels = [],
    $scope.selected = [],
    $scope.level = {},
    $scope.wallet = {},
    $scope.user = $localStorage.user,
    $scope.title = "Upgrade User",
    $scope.messeger = "",
    $scope.popup = function (windowClass) {
        $modal.open({
            windowClass: windowClass,
            templateUrl: "WarningModal",
            controller: "IModalInstanceCtrl",
            size: "sm",
            resolve: {
                messeger: function () {
                    return $scope.messeger;
                },
                title: function () {
                    return $scope.title;
                }
            }
        })
    },
    WalletWithBalance($scope.user.UserId),
    GetUserLevel();
    function WalletWithBalance(UserId) {
        commonFactory.GetNotPeddingWalletList(UserId).then(function (response) {            
            $scope.WalletList = response.data.Result;
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
    };
    function GetUserLevel() {
        commonFactory.GetUserLevel().then(function (response) {
            $scope.UserLevels = response.data.Result;
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
    };
    $scope.doUpgrade = function (data) {
        if (!angular.isUndefined(data) && data.LevelId > 0 && data.WalletId > 0) {
            $http.post($rootScope.APIUrl + 'UpgradeUser', { UserId: $scope.user.UserId, LevelId: data.LevelId, walletId: data.WalletId, Period: 1})
                .then(function successCallback(resp) {
                    if (resp.data.Status) {
                        $scope.WalletList = resp.data.Result;
                        $scope.messeger = resp.data.Messeger,
                        $scope.popup("modal-message modal-success");
                        //});
                    } else {                        
                        $scope.messeger = resp.data.Messeger,
                        $scope.popup("modal-message modal-warning");
                    }
                }
            )
        } else {            
            $scope.messeger = "Input data is not validation",
            $scope.popup("modal-message modal-warning");
        }
    }
})
app.controller('IModalInstanceCtrl', function ($scope, $modalInstance, messeger, title) {
    $scope.messeger = messeger;
    $scope.title = title;
    $scope.ok = function () {
        $modalInstance.close();
    };
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
});